package com.bgy.config.jwt;

import com.bgy.config.jwt.bean.OperInfoVo;
import com.bgy.config.jwt.help.UserDao;
import com.bgy.common.exception.CommonException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JwtUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserDao userDao;

    @Override
    public JwtUser loadUserByUsername(String username) throws CommonException {

        OperInfoVo operInfoVo = userDao.findByUsername(username);
        List<String> roles = operInfoVo.getRoles();
        if (roles == null || roles.size() == 0) {
            throw new CommonException("400", "权限不足");
        }
        return JwtUserFactory.create(operInfoVo);
    }
}
